<?php

namespace App\Http\Services;

use App\Http\Model\Prop;
use App\Lib\CommonSystemMsg;
use App\Lib\rc;

class CommonSystemMsgServices extends CommonServices
{
    /**
     * 融云实例
     *
     * @var rc
     */
    private $libRCloud;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->libRCloud = new rc();//实例化融云
    }

    /**
     * 通用发系统消息, k值需要在app/Lib/CommonSystemMsg中定义好，方便查找
     * 消息未拉平
     *
     * @param int $toUserId 目标用户id
     * @param int $k k值
     * @param array $data 数据
     * @return bool
     */
    public function sendSystemMsgToUser(int $toUserId, int $k, array $data): bool
    {
        if (!in_array($k, CommonSystemMsg::getK())) {
            return false;
        }

        $data = ['k' => $k, 'data' => $data,];
        return $this->libRCloud->send_system_msg(11, $toUserId, 'kk:common_system_msg', $data);
    }

    /**
     * 通用发系统消息, k值需要在app/Lib/CommonSystemMsg中定义好，方便查找
     * 消息要拉平
     *
     * @param int $toUserId 目标用户user_id
     * @param int $k k值
     * @param array $data 数据
     * @param string $objectName
     * @return bool
     */
    public function sendSystemMsgToUserV2(int $toUserId, int $k, array $data, string $objectName = 'kk:common_system_msg'): bool
    {
        if (!in_array($k, CommonSystemMsg::getK())) {
            return false;
        }

        $data = array_merge(['k' => $k], $data);
        return $this->libRCloud->send_system_msg(11, $toUserId, $objectName, $data);
    }

    /**
     * 发道具
     *
     * Created at 2021/11/8 19:29
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $toUserid
     * @param int $propId
     */
    public function sendPropSystemMsg(int $toUserid, int $propId)
    {
        $propInfo = (new Prop())->getPropById($propId);
        if (!$propInfo) {
            return;
        }

        $propName = $propInfo['name'];
        (new MsgServices())->sendPropMsg($toUserid, '道具【' . $propName . '】已发放到您的道具背包');
    }
}